﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace M3U8下载神器
{
    public partial class NewTaskDialog : Form
    {
        public M3U8Package Package { set; get; }
        public string SavePath
        {
            set => textBox2.Text = value;
            get => textBox2.Text;
        }
        public string Url
        {
            set=> textBox1.Text=value;
            get => textBox1.Text;
        }
        public void SetFileNameToSavePath(string name)
        {
            SavePath=Path.GetDirectoryName(SavePath)+ "\\" + name+ ".ts";
        }
        public NewTaskDialog()
        {
            InitializeComponent();
            SavePath = Path.GetFullPath( User.Default.LastDir) +"\\"+ new Random().Next() + ".ts";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            var url = Url;
            bool free=ThreadPool.QueueUserWorkItem(new WaitCallback((o)=> 
            {
                button1.Invoke(new Action(() => { button1.Enabled = false; }));
                try
                {
                    var try_times = 5;
                    Begin:
                    string m3u8 = SimpleDownloader.Download2(url);
                    if (m3u8 != null)
                    {
                        var package = M3U8Package.Parse(url, m3u8);
                        if (package != null)
                        {
                            if (package.StreamInfos.Count > 0)
                            {
                                var streamPath = package.StreamInfos[0];
                                url = streamPath.ToLower().StartsWith("http") ? streamPath :( package.Host + streamPath);
                                if (try_times-- > 0)
                                {
                                    goto Begin;
                                }
                            }
                            package.SavePath = SavePath;
                            Package = package;
                            User.Default.LastDir = Path.GetDirectoryName(SavePath);
                            DialogResult = DialogResult.OK;
                            //return;
                        }
                    }
                    MessageBox.Show(this, "连接超时!"); ;
                }
                catch (Exception) 
                {

                }
                if(DialogResult!=DialogResult.OK)
                button1.Invoke(new Action(() => {button1.Enabled = true; }));
            }),null);
            if (!free)
            {
                MessageBox.Show("工作繁忙，请稍后再试！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.DefaultExt = ".ts";
            saveFileDialog.Filter = "Ts|*.ts";
            saveFileDialog.FileName = Path.GetFileName(SavePath);
            saveFileDialog.InitialDirectory = Path.GetDirectoryName(SavePath);
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                SavePath = saveFileDialog.FileName;
            }
        }

    }
}
